package org.yylnb.openplatform.monitor.config;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.yylnb.openplatform.monitor.job.AvgJob;

/**
 * @author RainLin
 * @date 2020/8/3 - 19:14
 */
@Configuration
public class JobConfig {
    @Bean(initMethod = "init")
    public ZookeeperRegistryCenter zookeeperRegistryCenter(@Value("${job.zookeeper.serverList}") String serverList,
                                                           @Value("${job.zookeeper.nameSpace}") String nameSpace) {
        ZookeeperConfiguration configuration=new ZookeeperConfiguration(serverList,nameSpace);
        configuration.setConnectionTimeoutMilliseconds(100000);
        configuration.setSessionTimeoutMilliseconds(100000);
        //设置最大重试次数
        configuration.setMaxRetries(10);
        ZookeeperRegistryCenter zookeeperRegistryCenter = new ZookeeperRegistryCenter(configuration);
        return zookeeperRegistryCenter;

    }

    @Bean
    public LiteJobConfiguration liteJobConfiguration(@Value("${job.corn}") String corn,
                                                     @Value("${job.sharedCountmm}") int sharedCountmm,
                                                     @Value("${job.shardingItem}") String shardingItem) {
        JobCoreConfiguration coreConfiguration = JobCoreConfiguration.newBuilder(AvgJob.class.getName(), corn, sharedCountmm).shardingItemParameters(shardingItem).build();
        LiteJobConfiguration configuration = LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(coreConfiguration,AvgJob.class.getCanonicalName())).overwrite(true).build();
        return configuration;
    }

    @Bean(initMethod = "init")
    public SpringJobScheduler springJobScheduler(ZookeeperRegistryCenter zookeeperRegistryCenter,
                                                 LiteJobConfiguration liteJobConfiguration,
                                                 AvgJob avgJob) {
        return new SpringJobScheduler(avgJob, zookeeperRegistryCenter, liteJobConfiguration);
    }


}
